// import routes from "./modules/router.js";
import { createRouter, createWebHashHistory } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
// import store from "../store";

const files = import.meta.globEager("./modules/**", { eager: true });
const routes = []; // 保存页面路由
for (const path in files) {
  const module = files[path];
  // 在这里处理导入的模块
  routes.push(...module.default);
}

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

NProgress.configure({
  ease: "ease",
  speed: 500,
});

const writeNames = ["/login", "/reset"];
router.beforeEach((to, from, next) => {
  NProgress.start();
  if (sessionStorage.getItem("token")) {
    next();
  } else {
    if (writeNames.includes(to.path)) {
      next();
    } else {
      next("/login");
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});

const sortRoute = routes.sort((a, b) => a.sort - b.sort);
export { sortRoute };

export default router;
